// pages/operationRecord/operationRecord.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    record_data: [],
    page: 1, //当前页数
    totil: 1, //总页数
    pageSize: 15,
    noMore: false,
    loading: false,
    water_id: '',
    empty: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      water_id: options.water_id
    })
    this.operationList();
  },


  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    var page = that.data.page + 1;
    if (page > that.data.totil) {
      that.setData({
        noMore: true
      })
      return false;
    }
    that.setData({
      page: page
    })
    that.operationList();
  },
  //获取操作记录列表
  operationList: function () {
    var that = this;
    app.ajax({
      url: 'Partner/WaterCard/card_operation_mobile',
      method: "POST",
      data: {
        page: that.data.page,
        row: that.data.pageSize,
        water_id: that.data.water_id,
        type: '',
        start_time: '',
        end_time: ''
      },
      success: function (res) {
        var flag = res.data;
        //console.log(flag);
        if (res.data.code == 1000) {
          let len = 0;
          for (let i = 0; i < flag.data.data.length; i++) {
            len += flag.data.data[i].child.length;
          }
          // console.log(len)
          if (that.data.pageSize > len) {
            that.setData({
              noMore: true
            })
          } else {
            that.setData({
              noMore: false
            })
          }
          if (flag.data.data.length != 0 && that.data.record_data.length != 0) {
            if (that.data.record_data[that.data.record_data.length - 1].date == flag.data.data[0].date) {
              var tmp = that.data.record_data;
              var same_time = tmp[tmp.length - 1].child.concat(flag.data.data[0].child);
              tmp[tmp.length - 1].child = same_time;
              that.setData({
                record_data: tmp
              });
              flag.data.data.splice(0, 1);
            }
          }
          var record = that.data.record_data.concat(flag.data.data)
          that.setData({
            record_data: record,
            totil: flag.data.page.page_count,
            empty: flag.data.page.page_count == 0 ? true : false
          });
        } else {
          // app.showToast('暂无数据');
          that.setData({
            noMore: true
          })
        }
      }
    })
  }

})